package cn.com.twoke.game.client.games.tic_tac_toe.handler;

import cn.com.twoke.game.client.GameClientManager;
import cn.com.twoke.game.client.cache.ClientCacheManager;
import cn.com.twoke.game.client.games.gomoku.GomokuGame;
import cn.com.twoke.game.client.games.gomoku.entity.Chess;
import cn.com.twoke.game.client.games.tic_tac_toe.TicTacToeGame;
import cn.com.twoke.game.client.games.tic_tac_toe.constants.TicTacToeChess;
import cn.com.twoke.game.client.games.tic_tac_toe.constants.TicTacToeGameConstant;
import cn.com.twoke.game.client.games.tic_tac_toe.entity.data.DropData;
import cn.com.twoke.game.client.games.tic_tac_toe.entity.data.UndoData;
import cn.com.twoke.game.common.entity.message.GameDataMessage;
import cn.com.twoke.game.common.handler.GameDataHandler;

import javax.swing.*;

public class TicTacToeGameDataHandler  implements GameDataHandler, TicTacToeGameConstant {

    private TicTacToeGame game;
    private int[][] board = new int[ROW][COL];

    @Override
    public void handle(GameDataMessage message) {
        this.game = (TicTacToeGame) ClientCacheManager.INSTANCE.getGame(TicTacToeGame.ID);
        if (message.getData() instanceof DropData) {
            handle((DropData) message.getData());
        } else if (message.getData() instanceof UndoData) {
            handle((UndoData) message.getData());
        }
    }

    private void handle(DropData data) {
        this.game.drop(data.getX(), data.getY(), data.getType());
        if (this.game.checkWin(data.getX(), data.getY(), data.getType())) {
            JOptionPane.showMessageDialog(this.game.getWindow(), "玩家["+data.getFromId()+"]获胜!");
            this.game.resetGame();
            TicTacToeChess.Type notUserDropingChessType = this.game.getNotUserDropingChessType();
            if (notUserDropingChessType == TicTacToeChess.Type.CROSS) {
                this.game.setCanDropChess(false);
            } else {
                this.game.setCanDropChess(true);
            }
            this.game.setDroppingChessType(notUserDropingChessType);
            return;
        }
        for (int _y = 0; _y < ROW; _y++) {
            for (int _x = 0; _x < COL; _x++) {
                board[_y][_x] =this.game.getGrid()[_y][_x].getType().getCode();
            }
        }
        if (isGameOver()) {
            JOptionPane.showMessageDialog(this.game.getWindow(), "平局!");
            this.game.resetGame();
            TicTacToeChess.Type notUserDropingChessType = this.game.getNotUserDropingChessType();
            if (notUserDropingChessType == TicTacToeChess.Type.CROSS) {
                this.game.setCanDropChess(false);
            } else {
                this.game.setCanDropChess(true);
            }
            this.game.setDroppingChessType(notUserDropingChessType);
        }
        if (data.getFromId().equals(this.game.getPlayerName())) {
            this.game.setCanDropChess(false);
        } else {
            this.game.setCanDropChess(true);
        }
    }


    private boolean isGameOver() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == 0) return false;
            }
        }
        return true;
    }

    private void handle(UndoData data) {
        if (data.getFromId().equals(this.game.getPlayerName()) && null != data.getAcceptId()) {
            if (data.isAccept()) {
                this.game.getChessNotes().pop().setType(TicTacToeChess.Type.NONE);
                JOptionPane.showMessageDialog(this.game.getWindow(),
                        "玩家["+data.getAcceptId()+"]同意撤销上一步棋!");
            } else {
                JOptionPane.showMessageDialog(this.game.getWindow(), "玩家["+data.getAcceptId()+"]拒绝撤销上一步棋!");
            }
        }
        if (!data.getFromId().equals(this.game.getPlayerName()) && null == data.getAcceptId()) {
            int i = JOptionPane.showConfirmDialog(this.game.getWindow(), "玩家[" + data.getFromId() + "]请求撤销上一步棋");
            if (i == JOptionPane.OK_OPTION) {
                this.game.getChessNotes().pop().setType(TicTacToeChess.Type.NONE);
                data.setAccept(true);
            }
            data.setAcceptId(this.game.getPlayerName());
            GameClientManager.INSTANCE.sendGameData(data, GomokuGame.ID, this.game.getRoomId());
        }
    }

}
